package com.example.mobile2021606177cke1;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private Button btnMainGys;
    private Button btnMianYcl;
    private TextView tvMainResult;
    private Button btnMianClose;
    private Button btnMianLeave;
    private  String[]parts=new String[]{"轿车引擎","轿车发动机","轿车方向盘"};
    private ListView listView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainGys.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[]supplies={"新星汽车配件","枫叶汽车配件","天慧汽车配件","永辉汽车配件"};
                new AlertDialog.Builder(MainActivity.this).setTitle("请选择供应商").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                })
                        .setSingleChoiceItems(supplies, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i) {
                                Toast .makeText(MainActivity.this,"已选中"+supplies[i],Toast.LENGTH_LONG).show();
                                tvMainResult.setText(supplies[i]);
                            }
                        })
                        .create().show();
            }
        });
        btnMianYcl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog;
                final AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                builder.setMultiChoiceItems(parts, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {

                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        tvMainResult.setText(null);
                        for (int i=0;i<parts.length-1;i++){
                            if (listView.getCheckedItemPositions().get(i));{
                                tvMainResult.append("已加购："+parts[i]+"\n"+"\n");
                            }
                        }
                        AlertDialog dialog1;
                        AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
                        builder.setTitle("加购成功");
                        builder.setMessage("订单已创建，请再规定时间内付款哦亲");
                        builder.setPositiveButton("好的呢",null);
                        builder.create();
                        builder.show();
                    }
                });
                dialog=builder.create();
                listView=dialog.getListView();
                builder.show();
            }
        });
             btnMianLeave.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     new AlertDialog.Builder(MainActivity.this)
                             .setTitle("官方提示信息")
                             .setMessage("确定要清空你的购物车吗")
                             .setNegativeButton("取消",null)
                             .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                 @Override
                                 public void onClick(DialogInterface dialog, int which) {
                                     tvMainResult.setText(null);
                                     Toast.makeText(MainActivity.this,"购物车已清空",Toast.LENGTH_SHORT).show();

                                 }
                             })
                             .create().show();
                 }
             });
             btnMianClose.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     Toast.makeText(MainActivity.this,"退出成功",Toast.LENGTH_SHORT).show();
                     MainActivity.this.finish();
                 }
             });
    }

    private void initView() {
        tvMainTitle = findViewById(R.id.tv_main_title);
        btnMainGys = findViewById(R.id.btn_main_gys);
        btnMianYcl = findViewById(R.id.btn_mian_ycl);
        tvMainResult = findViewById(R.id.tv_main_result);
        btnMianClose = findViewById(R.id.btn_mian_close);
        btnMianLeave = findViewById(R.id.btn_mian_leave);
    }
}